<template>
  <div>
    <LoginHeader></LoginHeader>
    <div class="main">
      <div class="login-box">
      <div class="title">
        <b>注 册</b>
      </div>
      <div class="form">
        <el-form
          ref="formRef"
          :model="user"
          :rules="rules"
          label-position="right"
          label-width="80px"
          status-icon
        >
          <el-form-item label="账号" prop="username">
            <el-input type="text" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" style="margin-top: 25px" prop="confirmPassword">
            <el-input
              v-model.trim="user.confirmPassword"
              show-password
              aria-required="true"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="onSubmit" style="background-color: #27BA9B;color: #ffff; width: 100%;">注册</el-button>
            <!-- <el-button @click="$router.push('/login')" style="width: 35%;">返回</el-button> -->
          </el-form-item>
        </el-form>
      </div>
      </div>
    </div>
    <LoginFooter></LoginFooter>
  </div>
</template>

<script>
import LoginHeader from './Login/components/loginHeader.vue'
import LoginFooter from './Login/components/loginFooter.vue'
export default {
  name: "Login",
  components: {LoginHeader, LoginFooter},
  data() {
    return {
      user: {},
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 14, message: '密码长度为6-14个字符', trigger: 'blur' },
        ],
        confirmPassword: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
        ]
      }
    
    };
  },
  methods: {
    onSubmit() {
      if (
        this.user.username === "" ||
        this.user.password === "" ||
        this.user.confirmPassword === ""
      ) {
        this.$message.error("账号或密码不能为空");
        return false;
      }
      if (this.user.password !== this.user.confirmPassword) {
        this.$message.error("两次密码不一致");
        return false;
      }
      this.request.post("/register", this.user).then((res) => {
        if (res.code === "200") {
          this.$message.success("注册成功");
          this.$router.push("/login");
        } else {
          this.$message.error(res.msg);
        }
      });
    },
  },
};
</script>

<style scoped lang="less">
.main{
  width: 100%;
  height: 488px;
  background: url(../assets/images/login-bg.png) no-repeat center / cover;
  position: relative;
}
.login-box {
  margin: 0 auto;
  padding: 20px;
  position: absolute;
  left: 50%;
  top: 54px;
  width: 380px;
  background-color: #ffffff;
  .form{
    margin: 30px 10px 20px 10px;
  }
}
.title {
  text-align: center;
  margin-top: 10px;
  font-size: 20px;
  color: #42b983;
}
</style>
